﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using AspNetExpressInformationManageSystem.Models;

namespace AspNetExpressInformationManageSystem.Controllers
{
    [Authorize(Roles = "Distributor")]
    public class ExpressDispatchesController : Controller
    {
        private ExpressDbContext db = new ExpressDbContext();

        // GET: ExpressDispatches
        public ActionResult Index()
        {
            var expressDispatches = db.ExpressDispatches.Include(e => e.ExpressGoods).Include(e => e.ExpressVehicle);
            return View(expressDispatches.ToList());
        }

        // GET: ExpressDispatches/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressDispatch expressDispatch = db.ExpressDispatches.Find(id);
            if (expressDispatch == null)
            {
                return HttpNotFound();
            }
            return View(expressDispatch);
        }

        // GET: ExpressDispatches/Create
        public ActionResult Create()
        {
            ViewBag.ExpressGoodsId = new SelectList(db.ExpressGoods, "ID", "ID");
            ViewBag.ExpressVehicleID = new SelectList(db.ExpressVehicles, "ID", "PlateNum");
            return View(new ExpressDispatch { Date = DateTime.Now.Date });
        }

        // POST: ExpressDispatches/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,ExpressGoodsId,ExpressVehicleID,Fromaddr,Toaddr,Date")] ExpressDispatch expressDispatch)
        {
            if (ModelState.IsValid)
            {
                db.ExpressDispatches.Add(expressDispatch);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ExpressGoodsId = new SelectList(db.ExpressGoods, "ID", "ID", expressDispatch.ExpressGoodsId);
            ViewBag.ExpressVehicleID = new SelectList(db.ExpressVehicles, "ID", "PlateNum", expressDispatch.ExpressVehicleID);
            return View(expressDispatch);
        }

        // GET: ExpressDispatches/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressDispatch expressDispatch = db.ExpressDispatches.Find(id);
            if (expressDispatch == null)
            {
                return HttpNotFound();
            }
            ViewBag.ExpressGoodsId = new SelectList(db.ExpressGoods, "ID", "ID", expressDispatch.ExpressGoodsId);
            ViewBag.ExpressVehicleID = new SelectList(db.ExpressVehicles, "ID", "PlateNum", expressDispatch.ExpressVehicleID);
            return View(expressDispatch);
        }

        // POST: ExpressDispatches/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,ExpressGoodsId,ExpressVehicleID,Fromaddr,Toaddr,Date")] ExpressDispatch expressDispatch)
        {
            if (ModelState.IsValid)
            {
                db.Entry(expressDispatch).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ExpressGoodsId = new SelectList(db.ExpressGoods, "ID", "ID", expressDispatch.ExpressGoodsId);
            ViewBag.ExpressVehicleID = new SelectList(db.ExpressVehicles, "ID", "PlateNum", expressDispatch.ExpressVehicleID);
            return View(expressDispatch);
        }

        // GET: ExpressDispatches/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressDispatch expressDispatch = db.ExpressDispatches.Find(id);
            if (expressDispatch == null)
            {
                return HttpNotFound();
            }
            return View(expressDispatch);
        }

        // POST: ExpressDispatches/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ExpressDispatch expressDispatch = db.ExpressDispatches.Find(id);
            db.ExpressDispatches.Remove(expressDispatch);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
